<?php if (!defined('BASE_PATH')) exit('Not allowed.');

class Application
{
	var $uri;
	var $model;
	var $connection;
	var $dao;
	var $permission;
	var $path;
	
	function __construct($uri)
	{
		require_once 'application/controller/SessionController.php';
		
		//istanzio la sessione
    	//carico la view della barra
		$this->path= $uri['path'] ;
		//
		//verifco i permessi
		require_once 'application/controller/permissionController.php';
		if($uri!=array()){
			$p=permissionController::singleton();
			
			$this->permission=$p->permission($uri);

		}
		$this->uri = $uri;

	}

	function loadController($class)
	{
		if($this->uri['controller']=="index.php"){
		$this->redirect("Login");
		die();
		}
		$file = "application/controller/".$this->uri['controller'].".php";
		if(!file_exists($file)){
			$this->loadView('view_pageNotFound');
			die();
		}
		require_once($file);

		

	if($this->permission || $class=="Login" ){

			$controller = new $class();

			if(method_exists($controller, $this->uri['method']))
			{
				if(isset($_GET)|| $_GET!=array()){
				 foreach ($_GET as $v)
					$var[]=$v;
				}else
				$var= explode('/',$this->uri['var']);
				//$controller->{$this->uri['method']}($var[0],$var[1],$var[2],$var[3],$var[4],$var[5],$var[6],$var[7],$var[8],$var[9],$var[10],$var[0],$var[11],$var[12]);
				call_user_func_array(array($controller, $this->uri['method']),$var);
			} else {
				$controller->View_index();
			}
		}else 
		$this->loadView('view_NotAllow');

	}

	function loadView($view,$vars="")
	{
		if(is_array($vars) && count($vars) > 0)
		extract($vars, EXTR_PREFIX_SAME, "wddx");
		require_once('view/'.$view.'.php');
	}

	function loadModel($model)
	{
		require_once('model/'.$model.'.php');
		$this->$model = new $model;
	}
	function loadConnection($dao)
	{
		require_once('dao/Dao.php');
		require_once('dao/DataAccess.php');
		$this->connection=new DataAccess(HOST,USERNAME,PASSWORD,DBNAME);
		require_once('dao/'.$dao.'.php');
		$this->dao = new $dao($this->connection);
	}
	public function redirect($controller,$method = "",$args = array())
	{
		
		if($args==array())
		$location = BASE_PATH . $controller."/".$method;
		else{
			$arg=implode ("&" , $args );
		$location = BASE_PATH . $controller."/".$method."/?".$arg;
		}
		/*
		 * Use @header to redirect the page:
		 */
		header("Location: " . $location);
		exit;
	}
  function build_menu ( $menu )
	{
     
		$out .= "\n".'<ul id="mega-1" class="mega-menu">' . "\n";
		
		for ( $i = 1; $i <= count ( $menu ); $i++ )
		{
			if ( is_array ( $menu [ $i ] ) ) {//must be by construction but let's keep the errors home
				if ( $menu [ $i ] [ 'show_condition' ] && $menu [ $i ] [ 'parent' ] == 0 ) {//are we allowed to see this menu?
					
					$out .= '<li><a href="' . $menu [ $i ] [ 'link' ] . '">';
					$out .= $menu [ $i ] [ 'text' ];
					$out .= '</a>';
					$out.="<ul>";
					$out .= $this->get_childs ( $menu, $i );
					$out.="</ul>";
					$out .= '</li>' . "\n";
				}
			}
			else {
				die ( sprintf ( 'menu nr %s must be an array', $i ) );
			}
		}
		
		$out .= '</ul>'."\n";
		$out .= "\n\t" . '</div>';
		return $out . "\n\t" . '</div>';
	}
	
	function get_childs ( $menu, $el_id )
	{
		$has_subcats = FALSE;
		$out .= "\n".'	<ul>' . "\n";
		
		for ( $i = 1; $i <= count ( $menu ); $i++ )
		{
			if ( $menu [ $i ] [ 'show_condition' ] && $menu [ $i ] [ 'parent' ] == $el_id ) {//are we allowed to see this menu?
				$has_subcats = TRUE;
				$add_class = ( $this->get_childs ( $menu, $i ) != FALSE ) ? ' subsubl' : '';
				$out .= '		<li><a href="' . $menu [ $i ] [ 'link' ] . '">';
				$out .= $menu [ $i ] [ 'text' ];
				$out .= '</a>';
				$out .= $this->get_childs ( $menu, $i );
				$out .= '</li>' . "\n";
			}
		}
		$out .= '	</ul></li>'."\n";
		return ( $has_subcats ) ? $out : FALSE;
	}
	
}

?>